<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Notifications\AdminMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use Inertia\Inertia;

class AdminNotificationController extends Controller
{
    // 显示发送消息页面
    public function create()
    {
        $users = User::select('id', 'name', 'email')
            ->orderBy('name')
            ->get();
            
        return Inertia::render('Admin/Notifications/Send', [
            'users' => $users
        ]);
    }
    
    // 发送通知给用户
    public function send(Request $request)
    {
        $request->validate([
            'user_ids' => 'required|array',
            'user_ids.*' => 'exists:users,id',
            'title' => 'required|string|max:100',
            'content' => 'required|string|max:500',
            'url' => 'nullable|url'
        ]);
        
        $users = User::whereIn('id', $request->user_ids)->get();
        
        Notification::send($users, new AdminMessage(
            $request->title,
            $request->content,
            $request->url
        ));
        
        return back()->with('success', '消息发送成功');
    }
}
